package com.easondongh.system.controller;

import com.easondongh.system.domain.SysPermission;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysPermissionService;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.vo.PermissionQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/9 14:31
 */
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @GetMapping("/loadAllPermission")
    public ResponseObj<List<SysPermission>> loadAllPermission() {
        List<SysPermission> list = this.permissionService.list();
        return new ResponseObj<List<SysPermission>>(ResponseEnum.QUERY_SUCCESS, list);
    }

    /**
     * 分页查询所有权限
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<SysPermission>> getByPage(@RequestBody BasePageQueryVo<PermissionQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<SysPermission> result = this.permissionService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    @PostMapping("/addPermission")
    public ResponseObj addPermission(@RequestBody SysPermission permission) {
        if(permission == null || StringUtils.isBlank(permission.getModule()) ||  StringUtils.isBlank(permission.getOperationType())) {
            log.error("新增权限时模块名、操作类型必填");
            return ResponseObj.AddFail;
        }
        this.permissionService.save(permission);
        return ResponseObj.AddSuccess;
    }

    @PutMapping("/updateById")
    public ResponseObj updateById(@RequestBody SysPermission permission) {
        if(permission == null || permission.getId() == null || StringUtils.isBlank(permission.getModule()) ||  StringUtils.isBlank(permission.getOperationType())) {
            log.error("修改权限时id、模块名、操作类型必填");
            return ResponseObj.UpdateFail;
        }
        this.permissionService.updateById(permission);
        return ResponseObj.UpdateSuccess;
    }

    @DeleteMapping("/deleteById")
    public ResponseObj deleteById(Integer id) {
        if(id == null) {
            return ResponseObj.DeleteFail;
        }
        this.permissionService.removeById(id);
        return ResponseObj.DeleteSuccess;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.permissionService.removeByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }
}
